use nalgebra::Point2;
use crate::tank::Tank;

// 炮弹结构
pub struct Bullet {
    pub position: Point2<f32>,
    pub direction: Point2<f32>,
    pub speed: f32,
    pub damage: f32,
    pub owner: usize,
    pub max_distance: f32,
    pub distance_traveled: f32,
    pub active: bool, // 添加激活状态
}

impl Bullet {
    pub fn new(position: Point2<f32>, direction: Point2<f32>, owner: usize) -> Self {
        Bullet {
            position,
            direction,
            speed: 9.0,
            damage: 30.0,
            owner,
            max_distance: 250.0,
            distance_traveled: 0.0,
            active: true,
        }
    }
    
    // 更新炮弹位置
    pub fn update(&mut self, dt: f32) -> bool {
        if !self.active {
            return false;
        }
        
        let movement = self.direction * self.speed * dt;
        self.position.x += movement.x;
        self.position.y += movement.y;
        self.distance_traveled += movement.coords.magnitude();
        
        // 如果炮弹超出最大距离，则需要销毁
        self.active = self.distance_traveled < self.max_distance;
        self.active
    }
    
    // 检查是否击中目标
    pub fn check_collision(&self, target: &Tank) -> bool {
        let distance = (self.position - target.position).magnitude();
        distance < 20.0 // 简单的碰撞检测
    }
}

// 碰撞系统
pub struct CollisionSystem;

impl CollisionSystem {
    // 检查炮弹与坦克的碰撞
    pub fn check_bullet_tank_collision(bullet: &Bullet, tank: &Tank) -> bool {
        let distance = (bullet.position - tank.position).magnitude();
        distance < 20.0 // 简单的碰撞检测半径
    }
    
    // 检查坦克与道具的碰撞
    pub fn check_tank_prop_collision(tank: &Tank, prop_position: Point2<f32>) -> bool {
        let distance = (tank.position - prop_position).magnitude();
        distance < 25.0
    }
}